package com.minicode.qr.activities;

import java.io.IOException;
import java.net.MalformedURLException;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.minicode.facebook.DialogError;
import com.minicode.facebook.Facebook;
import com.minicode.facebook.Facebook.DialogListener;
import com.minicode.facebook.FacebookError;
import com.minicode.facebook.util.FacebookUtil;
import com.minicode.foursqare.Foursquare;
import com.minicode.foursqare.Foursquare.foursquareDialogListener;
import com.minicode.foursqare.FoursquareError;
import com.minicode.foursqare.util.FoursquareUtil;
import com.minicode.google.Google;
import com.minicode.google.GoogleError;
import com.minicode.google.Google.GoogleDialogListener;
import com.minicode.google.util.GoogleUtil;
import com.minicode.qr.R;
import com.minicode.twitter.Twitter;
import com.minicode.twitter.Twitter.TweetDialogListener;
import com.minicode.twitter.TwitterError;
import com.minicode.twitter.util.TwitterUtil;
import com.minicode.utils.AbstractActivity;

public class QRActivity extends AbstractActivity {

	/*
	 * Buttons
	 */
	private ImageButton imgbtnFacebookLogin;
	private ImageButton imgbtnTwitterLogin;
	private ImageButton imgbtnFoursquareLogin;
	private ImageButton imgbtnGoogleLogin;
	private Button btnSignUp;
	private Button btnSignIn;

	/*
	 * Vars
	 */
	public String sharedPreferencesName = "qrActivity.xml";
	private Facebook facebook;
	private Twitter twitter;
	private Foursquare foursquare;
	private Google google;

	/*
	 * Handler
	 */
	private Handler handleEvent = null;

	/*
	 * Constant
	 */
	public static String TAG = "QRActivity > ";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		/*
		 * init
		 */
		init(false);

		/*
		 * Avoid NetworkOnMainThreadException
		 */
		activeStrictMode();

		/*
		 * Init View
		 */
		setContentView(R.layout.main);

		/*
		 * Init Buttons
		 */
		imgbtnFacebookLogin = (ImageButton) findViewById(R.id.imgbtnFacebookLogin);
		imgbtnTwitterLogin = (ImageButton) findViewById(R.id.imgbtnTwitterLogin);
		imgbtnFoursquareLogin = (ImageButton) findViewById(R.id.imgbtnFoursquareLogin);
		imgbtnGoogleLogin = (ImageButton) findViewById(R.id.imgbtnGoogleLogin);

		btnSignIn = (Button) findViewById(R.id.btnSignIn);
		btnSignUp = (Button) findViewById(R.id.btnSignUp);

		/*
		 * Set button actions
		 */
		imgbtnFacebookLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (isAccessInternet()) {
					fbLogin();
				} else {
					Toast.makeText(getApplicationContext(),
							getText(R.string.internet_nointernet),
							Toast.LENGTH_SHORT).show();
				}
			}
		});

		imgbtnTwitterLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isAccessInternet()) {
					twLogin();
				} else {
					Toast.makeText(getApplicationContext(),
							getText(R.string.internet_nointernet),
							Toast.LENGTH_SHORT).show();
				}
			}
		});

		imgbtnFoursquareLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isAccessInternet()) {
					fsLogin();
				} else {
					Toast.makeText(getApplicationContext(),
							getText(R.string.internet_nointernet),
							Toast.LENGTH_SHORT).show();
				}
			}
		});

		imgbtnGoogleLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isAccessInternet()) {
					goLogin();
				} else {
					Toast.makeText(getApplicationContext(),
							getText(R.string.internet_nointernet),
							Toast.LENGTH_SHORT).show();
				}
			}
		});

		btnSignIn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(getApplicationContext(), "Test2",
						Toast.LENGTH_SHORT).show();
			}
		});

		btnSignUp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(getApplicationContext(), "Test2",
						Toast.LENGTH_SHORT).show();
			}
		});

		/*
		 * Init Others
		 */
		facebook = new Facebook(FacebookUtil.API_KEY);
		twitter = new Twitter(R.drawable.ic_tw_twitter);
		foursquare = new Foursquare(FoursquareUtil.FOURSQUARE_CLIENT_ID,
				FoursquareUtil.FOURSQUARE_CLIENT_SECRET,
				FoursquareUtil.FOURSQUARE_CALLBACK_URL);
		google = new Google(R.drawable.ic_go_google);
	}

	@Override
	protected void onResume() {
		super.onResume();
		System.gc();
	}

	/*
	 * Login Methods
	 */
	public void fbLogin() {

		/*
		 * Fb login request permissions
		 */

		facebook.authorize(this, FacebookUtil.PERMISIONS, new DialogListener() {

			public void onComplete(Bundle values) {
				try {
					JSONObject json = new JSONObject(facebook.request("me"));
					Log.e(TAG, json.getString("name"));
					// SavePreferencesString(FacebookUtil.FACEBOOKUSERNAME, json
					// .get("name").toString());
					// SavePreferencesString(FacebookUtil.FACEBOOKUSERID, json
					// .get("id").toString());
					// SavePreferencesString(FacebookUtil.FACEBOOKUSEREMAIL,
					// json
					// .get("email").toString());
					// SavePreferencesString(FacebookUtil.FACEBOOKUSERLINK, json
					// .get("link").toString());
					// SavePreferencesString(FacebookUtil.FACEBOOKACCESSTOKEN,
					// facebook.getAccessToken());

					json = null;
				} catch (MalformedURLException e) {
					Log.e(TAG + " MalformedURLException 1 ", e.toString());
					e.printStackTrace();
				} catch (JSONException e) {
					Log.e(TAG + " JSONException 2 ", e.toString());
					e.printStackTrace();
				} catch (IOException e) {
					Log.e(TAG + " IOException 3", e.toString());
					e.printStackTrace();
				}
			}

			public void onFacebookError(FacebookError error) {
				Log.e(TAG + " FacebookError 4", error.toString());
			}

			public void onError(DialogError e) {
				Log.e(TAG + " Dialog 5", e.toString());
			}

			public void onCancel() {
				Log.e(TAG, " Cancel 6");
			}
		});
	}

	public void twLogin() {
		twitter.authorize(this, handleEvent, TwitterUtil.CONSUMER_KEY,
				TwitterUtil.CONSUMER_SECRET, new TweetDialogListener() {

					public void onTwitterError(TwitterError e) {
						Log.e(TAG + " TwitterError 7 ", e.toString());
					}

					public void onComplete(Bundle values) {
						Log.e("Token : ", twitter.getAccessToken());
						Log.e("Secret : ", twitter.getSecretToken());
					}

					public void onCancel() {
						Log.e(TAG, " Cancel 8 ");
					}

					@Override
					public void onError(com.minicode.twitter.DialogError e) {
						Log.e(TAG + " Error 9 ", e.toString());
					}
				});

	}

	public void goLogin() {
		google.authorize(this, handleEvent, GoogleUtil.CLIENT_ID,
				GoogleUtil.CLIENT_SECRET, GoogleUtil.REDIRECT_URI,
				new GoogleDialogListener() {

					@Override
					public void onGoogleError(GoogleError e) {
						Log.e(TAG + " GoogleError 7 ", e.toString());

					}

					@Override
					public void onComplete(Bundle values) {
						Log.e("Token : ", google.getAccessToken());

					}

					@Override
					public void onCancel() {
						Log.e(TAG, " Cancel 8 ");

					}

					@Override
					public void onError(com.minicode.google.DialogError e) {
						Log.e(TAG + " Error 9 ", e.toString());

					}
				});

	}

	public void fsLogin() {
		foursquare.authorize(this, new foursquareDialogListener() {

			public void onFoursquareError(FoursquareError e) {
				Log.e(TAG + " FoursquareError 10 ", e.toString());

			}

			public void onComplete(Bundle values) {
				Log.e(TAG, foursquare.getAccessToken());
			}

			public void onCancel() {
				Log.e(TAG, " Cancel 11 ");

			}

			@Override
			public void onError(com.minicode.foursqare.DialogError e) {
				Log.e(TAG + " Error 12 ", e.toString());
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		facebook.authorizeCallback(requestCode, resultCode, data);
	}
}